package com.yanxibao.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanxibao.bean.University;
import com.yanxibao.common.Result;
import com.yanxibao.dto.PageQueryDTO;
import com.yanxibao.mapper.UniversityMapper;
import com.yanxibao.service.UniversityService;
import com.yanxibao.util.QueryUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 院校;(_university)表服务实现类
 *
 * @author kongke
 * @date 2023/11/16
 */
@Service
public class UniversityServiceImpl extends ServiceImpl<UniversityMapper, University> implements UniversityService {

    @Resource
    private UniversityMapper universityMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param universityId 主键
     * @return 实例对象
     */
    @Override
    public Result<University> queryById(String universityId) {
        University university = getById(universityId);
        return university == null ? Result.error("02000", "未找到对应数据")
                : Result.success(university);

    }

    /**
     * 分页查询
     *
     * @param university 筛选条件
     * @param current    当前页码
     * @param size       每页大小
     */
    @Override
    public Result<PageQueryDTO<University>> pageQuery(University university, Integer current, Integer size) {

        if (current == null || size == null || current < 1 || size < 1) {
            return Result.error("02010", "分页参数不正确");
        }

        Page<University> page = Page.of(current, size);
//        强制转换忽略检查，（已保证返回值不会出现冲突）
        @SuppressWarnings("unchecked")
        PageQueryDTO<University> dto =
                (PageQueryDTO<University>) QueryUtil.FltQuery("eq",university, page, universityMapper, null);

        return Result.success(dto);
    }

    /**
     * 查询全部
     *
     * @param university 大学
     * @return {@link Result}<{@link List}<{@link University}>>
     */
    @Override
    public Result<List<University>> queryAll(University university) {

//        强制转换忽略检查，（已保证返回值不会出现冲突）
        @SuppressWarnings("unchecked")
        List<University> list = (List<University>)
                QueryUtil.FltQuery("eq",university, null, universityMapper, null);

        return Result.success(list);
    }

    /**
     * 新增数据
     *
     * @param university 实例对象
     * @return 实例对象
     */
    @Override
    public Result<University> insert(University university) {

//      可用Redis重构
        String id = university.getUniversityId();
        University byId = getById(id);
        if (byId != null) {
            return Result.error("02031", "院校ID冲突");
        }

        boolean save = save(university);

        return save ? Result.success(university) : Result.error("02030", "新增失败");

    }

    /**
     * 更新数据
     *
     * @param university 实例对象
     * @return 实例对象
     */
    @Override
    public Result<University> update(University university) {
        boolean update = updateById(university);
        return update ? Result.success(university) : Result.error("02040", "更新失败");

    }

    /**
     * 通过主键删除数据
     *
     * @param universityId 主键
     * @return 是否成功
     */
    @Override
    public Result<Boolean> deleteById(String universityId) {

        boolean b = removeById(universityId);

        return b ? Result.success(true) : Result.error("02050", "删除失败");
    }


}